package cn.tedu.gateway.filter;

import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.Arrays;
import java.util.List;

@Component
public class LogGatewayFilterFactory extends AbstractGatewayFilterFactory<LogGatewayFilterFactory.Config> {

    //构造函数
    public LogGatewayFilterFactory(){
        super(Config.class);
    }

    /*绑定静态内部类，获取配置文件参数*/
    @Override
    public List<String> shortcutFieldOrder(){
        return Arrays.asList("consoleLog","cacheLog");
    }

    //过滤器的逻辑判断
    @Override
    public GatewayFilter apply(Config config){
        return new GatewayFilter() {
            @Override
            public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
                if(config.isConsoleLog()){
                    System.out.println("consoleLog已经开启......");
                }
                if(config.isCacheLog()){
                    System.out.println("cacheLog已经开启......");
                }
                return chain.filter(exchange);
            }
        };
    }

    //静态内部类，接收配置文件的参数
    public static class Config{
        private boolean consoleLog;
        private boolean cacheLog;

        public boolean isConsoleLog(){
            return consoleLog;
        }

        public void setConsoleLog(boolean consoleLog) {
            this.consoleLog = consoleLog;
        }
        public boolean isCacheLog(){
            return cacheLog;
        }

        public void setCacheLog(boolean cacheLog) {
            this.cacheLog = cacheLog;
        }
    }
}
